const config = require('../src/utils/config')

const { apiPrefix } = config

let friendStatistics = []
function generateDateEachHour() {
  let hour = new Date().getHours()
  if (hour === 0) {
    return { "0": 0 }
  } else {
    let data = {}
    for (let i = hour; i >= 0; i--) {
      data[i] = i
    }
    return data
  }
}

for (let i = 0; i < 10; i++) {
  let id = i + 1
  friendStatistics.push({
    id: id,
    avatar: i % 10 === 0 ? 'http://wx.qlogo.cn/mmhead/ver_1/4P7EyyQ4ibNptL57TN10jZeWFV2oGfnCHOlvFia0Ejy1ycJgxdcdIa9cgwVIQYhDdvVnC816elZFW84xTqiafKvjbzVKGO9dzbN6UMKwTn1X0U/132' : '',
    wxId: '重复好友微号' + id,
    employee: {
      name: '归属员工' + id,
      department: {
        name: '归属部门' + id
      }
    },
    promoter: {
      name: "推广员工" + id
    },
    contactCount: id,
    notPassFriend: id,
    period: id,
    twoWeekData: {
      ...generateDateEachHour()
    }
  })
}

module.exports = {
  [`GET ${apiPrefix}/friendStatistics/query`](req, res) {
    const { query } = req
    let { size, current, ...other } = query
    size = size || 10
    current = current || 1

    let newData = friendStatistics
    for (let key in other) {
      if ({}.hasOwnProperty.call(other, key)) {
        newData = newData.filter((item) => {
          if ({}.hasOwnProperty.call(item, key)) {
            return String(item[key]).trim().indexOf(decodeURI(other[key]).trim()) > -1
          }
          return true
        })
      }
    }

    res.status(200).json({
      code: 0,
      message: "successed",
      data: {
        records: newData,
        total: newData.length,
      },
    })
  },

  [`GET ${apiPrefix}/friendStatistics/queryStatisticalDiagram`](req, res) {
    const { query } = req
    let { size, current, type, ...other } = query
    size = size || 10
    current = current || 1
    let statisticalDiagram = []
    let fields = []
    for (let i = 0; i < 24; i += 1) {
      fields.push(`${i}-${i + 1}`);
    }
    if (type === 'gender') {
      statisticalDiagram = ["男", "女", "未知"].map(item => {
        let obj = {}
        obj.name = item
        fields.forEach((i, index) => obj[i] = index * 100)
        return obj
      })
    } else if (type === "addType") {
      statisticalDiagram = ["名片分享", "扫一扫", "搜索手机号", "搜索微信号", "主动添加", "其他"].map(item => {
        let obj = {}
        obj.name = item
        fields.forEach((i, index) => obj[i] = index * 100)
        return obj
      })
    } else {
      statisticalDiagram = ["未知"].map(item => {
        let obj = {}
        obj.name = item
        fields.forEach((i, index) => obj[i] = index * 100)
        return obj
      })
    }


    res.status(200).json({
      code: 0,
      message: "successed",
      data: {
        records: statisticalDiagram,
      },
    })
  },


  [`GET ${apiPrefix}/friendStatistics/chinaMapData`](req, res) {
    const { query } = req
    let { size, current, ...other } = query
    size = size || 10
    current = current || 1
    let data = [
      { name: '西藏', value: 0 },
      { name: '青海', value: 0 },
      { name: '宁夏', value: 0 },
      { name: '海南', value: 0 },
      { name: '甘肃', value: 0 },
      { name: '贵州', value: 57 },
      { name: '新疆', value: 66 },
      { name: '云南', value: 88 },
      { name: '重庆', value: 10 },
      { name: '吉林', value: 10 },
      { name: '山西', value: 11 },
      { name: '天津', value: 1 },
      { name: '江西', value: 11 },
      { name: '广西', value: 11 },
      { name: '陕西', value: 12 },
      { name: '黑龙江', value: 1 },
      { name: '内蒙古', value: 14 },
      { name: '安徽', value: 1 },
      { name: '北京', value: 1 },
      { name: '福建', value: 1 },
      { name: '上海', value: 19 },
      { name: '湖北', value: 1 },
      { name: '湖南', value: 1 },
      { name: '四川', value: 2 },
      { name: '辽宁', value: 2 },
      { name: '河北', value: 2 },
      { name: '河南', value: 2 },
      { name: '浙江', value: 3 },
      { name: '山东', value: 4 },
      { name: '江苏', value: 4 },
      { name: '广东', value: 5 },
    ]


    res.status(200).json({
      code: 0,
      message: "successed",
      data: {
        records: data,
      },
    })
  }
}
